<?php

class PageNavigator
{
    /**
     * 记录总数
     *
     * @access private
     * @var integer
     */
    private $_total;

    /**
     * 页面总数
     *
     * @access private
     * @var integer
     */
    private $_totalPage;

    /**
     * 当前页面
     *
     * @access private
     * @var integer
     */
    private $_currentPage;

    /**
     * 每页内容数
     *
     * @access private
     * @var integer
     */
    private $_pageSize;

    /**
     * 页面链接模板
     *
     * @access private
     * @var string
     */
    private $_pageTemplate;

    /**
     * 构造函数,初始化页面基本信息
     *
     * @access public
     * @param integer $total 记录总数
     * @param integer $page 当前页面
     * @param integer $pageSize 每页记录数
     * @param string $pageTemplate 页面链接模板
     * @return void
     */
    public function __construct($params)
    {
        $this->_total = $params['total'];
        $this->_totalPage = ceil($params['total'] / $params['pageSize']);
        $this->_currentPage = $params['currentPage'];
        $this->_pageSize = $params['pageSize'];
        $this->_pageTemplate = $params['pageTemplate'];
        $this->url = $params['url'];
    }
    
    /**
     * 输出盒装样式分页栏
     *
     * @access public
     * @return void
     */
    public function __toString()
    {
        if($this->_total < 1)
        {
            return '';
        }
        
        $prevWord = '上一页';
        $nextWord = '下一页';
        $splitPage = 5;
        $splitWord = '...';
        
        $string = "<a>总数：$this->_total</a>";
    
        $from = max(1, $this->_currentPage - $splitPage);
        $to = min($this->_totalPage, $this->_currentPage + $splitPage);

        //输出上一页
        if($this->_currentPage > 1)
        {
            $string .= '<a class="prev" href="' . str_replace('{page}', $this->_currentPage - 1, $this->_pageTemplate) . '">'
            .$prevWord . '</a>' . "\r\n";
        }

        //输出第一页
        if($from > 1)
        {
            $string .= '<a href="' . str_replace('{page}', 1, $this->_pageTemplate) . '">1</a>' . "\r\n";
            //输出省略号
            $string .= '<span>' . $splitWord . '</span>' . "\r\n";
        }

        //输出中间页
        for($i = $from; $i <= $to; $i ++)
        {
            if($i != $this->_currentPage)
            {
                $string .= '<a href="' . str_replace('{page}', $i, $this->_pageTemplate) . '">'
                . $i . '</a>' . "\r\n";
            }
            else
            {
                //当前页
                $string .= '<span class="current">' . $i . '</span>' . "\r\n";
            }
        }

        //输出最后页
        if($to < $this->_totalPage)
        {
            $string .= '<span>' . $splitWord . '</span>' . "\r\n";
            $string .= '<a href="' . str_replace('{page}', $this->_totalPage, $this->_pageTemplate) . '">'
            . $this->_totalPage . '</a>' . "\r\n";
        }

        //输出下一页
        if($this->_currentPage < $this->_totalPage)
        {
            $string .= '<a class="next" href="' . str_replace('{page}', $this->_currentPage + 1, $this->_pageTemplate) . '">'
            . $nextWord . '</a>' . "\r\n";
        }
        
        return $string;
    }
    function itemtpl()
    {
    	if($this->_total < 1)
        {
            return '';
        }
        
        $prevWord = '上一页';
        $nextWord = '下一页';
        $splitPage = 2;
        $splitWord = '...';
        
        $string = NULL;
    
        $from = max(1, $this->_currentPage - $splitPage);
        $to = min($this->_totalPage, $this->_currentPage + $splitPage + 1);

        //输出上一页
        if($this->_currentPage > 1)
        {
            $string .= '<a class="prev" href="' .  $this->url.($this->_currentPage-1). '">'
            .$prevWord . '</a>' . "\r\n";
        }

        //输出第一页
        if($from > 1)
        {
            $string .= '<a href="' . $this->url. '1">1</a>' . "\r\n";
            //输出省略号
            $string .= '<span>' . $splitWord . '</span>' . "\r\n";
        }

        //输出中间页
        for($i = $from; $i <= $to; $i ++)
        {
            if($i != $this->_currentPage)
            {
                $string .= '<a href="' . $this->url.$i . '">'
                . $i . '</a>' . "\r\n";
            }
            else
            {
                //当前页
                $string .= '<span class="current" style="color:black">' .$i. '</span>' . "\r\n";
            }
        }

        //输出最后页
        if($to < $this->_totalPage)
        {
            $string .= '<span>' . $splitWord . '</span>' . "\r\n";
            $string .= '<a href="' . $this->url.$this->_totalPage. '">'
            . $this->_totalPage . '</a>' . "\r\n";
        }

        //输出下一页
        if($this->_currentPage < $this->_totalPage)
        {
            $string .= '<a class="next" href="'.$this->url.($this->_currentPage + 1).'">'
            . $nextWord . '</a>' . "\r\n";
        }
        
        return $string;
    }
}
